package com.suray.wcs.service.plc;

import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.service.core.util.SocketSendUtil;
import com.suray.wcs.service.system.entity.DeviceDB;
import com.suray.wcs.service.system.service.impl.DeviceDBServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ScanCodeThread extends Thread {
    private static DeviceDBServiceImpl deviceDBService = SpringContextHolder.getBean(DeviceDBServiceImpl.class);

    private DeviceDB deviceDB;

    public ScanCodeThread(DeviceDB deviceDB) {

        this.deviceDB = deviceDB;
    }

    @Override
    public void run() {
        log.info(this.getName() + "扫码器线程启动: ");
        while (true) {
            try {
                SocketSendUtil socketSendUtil = SocketSendUtil.getSocketSendUtil(deviceDB.getIp(), deviceDB.getPort());
                byte[] codeStringbyte = socketSendUtil.getReturnBytes();
                String barcode = new String(codeStringbyte);
                deviceDB.setData(barcode);
                deviceDBService.updateById(deviceDB);
            } catch (IOException e) {
//                log.error(deviceDB.getName() + "扫码器网络连接异常", e);
            }
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
